﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Provider;
using PsTFS.Objects;
using PsTFS.Objects.Methodes;

namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Création d'un workspace
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Workspace")]
    public class Add_Workspace : PsCmdlet
    {
        /// <summary>
        /// Lancement du processus de création de workspace
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                string[] tabServerPath = ServerPath.Split('|');
                string[] tabLocalPath = LocalPath.Split('|');

                int nbServerPath = tabServerPath.Count();
                int nbLocalPath = tabLocalPath.Count();

                if (nbServerPath > 0 && nbLocalPath > 0 && nbServerPath == nbLocalPath)
                {
                    
                    List<WorkingFolder> workingFolders = new List<WorkingFolder>();

                    for (int i = 0; i < nbServerPath; i++)
                    {
                        WorkingFolder wf = new WorkingFolder(tabServerPath[i], tabLocalPath[i]);
                        workingFolders.Add(wf);
                    }

                    WriteObject(Methode.CreateWorkspace(Name, Owner, workingFolders.ToArray()));
                }
                else
                {
                    WriteObject(Methode.CreateWorkspace(Name, Owner, ServerPath, LocalPath));
                }

            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex,Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-Workspace is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS
        {
            set { Context.GetContext().Tfs = value; }
        }
        /// <summary>
        /// Nom du workspace
        /// </summary>
        [Parameter(Position = 1)]
        public string Name { get; set; }

        /// <summary>
        /// Propriétaire du workspace
        /// </summary>
        [Parameter(Position = 2)]
        public string Owner { get; set; }

        /// <summary>
        /// Emplacement serveur
        /// </summary>
        [Parameter(Position = 3)]
        public string ServerPath { get; set; }

        /// <summary>
        /// Emplacement local
        /// </summary>
        [Parameter(Position = 4)]
        public string LocalPath { get; set; }

    }
}